package com.snake.springboot.custom.controller.base;

import com.snake.springboot.custom.api.model.base.CustCertModel;
import com.snake.springboot.custom.api.model.base.CustomModel;
import com.snake.springboot.custom.api.request.base.CustomAdminReq;
import com.snake.springboot.custom.api.request.base.CustomQuery;
import com.snake.springboot.custom.api.request.base.CustomReq;
import com.snake.springboot.custom.api.request.page.PageQuery;
import com.snake.springboot.custom.api.response.base.CustomRsp;
import com.snake.springboot.custom.api.service.base.CustomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.snake.common.result.ResponseResult;
import org.snake.common.result.ResponseResultFactory;
import org.snake.common.result.ReturnList;
import org.snake.common.utils.IllegalStrUtils;
import org.snake.common.validate.Create;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>商户管理-商户信息
 * 类{@code CustomController}  </br>
 * @author yangxt
 * 创建时间2025/2/8 10:20
 */
@Api(tags = "商户管理-商户信息")
@RestController
@RequestMapping("/custom")
public class CustomController {

    private CustomService customService;
    @Autowired
    public CustomController(CustomService customService) {
        this.customService = customService;
    }

    @ApiOperation("新增商户信息")
    @PostMapping("/add")
    public ResponseResult<CustomReq> addCustom(
            @ApiParam(name = "接口入参", value = "传入json格式", required = true)
            @RequestBody @Validated({Create.class}) CustomReq customReq) {
        IllegalStrUtils.checkIllegalStrNotEmpty(customReq.getMainAccount().getAccount(),"登录名");
        CustomReq result = customService.addCustom(customReq);
        return ResponseResultFactory.ucc("添加成功！", result);
    }

    @ApiOperation("注册商户信息")
    @PostMapping("/register")
    public ResponseResult<CustomReq> register(
            @ApiParam(name = "接口入参", value = "传入json格式", required = true)
            @RequestBody @Validated({Create.class}) CustomReq customReq) {
        CustomReq result = customService.register(customReq);
        return ResponseResultFactory.ucc("添加成功！", result);
    }


    @ApiOperation("修改商户信息")
    @PostMapping("/modify")
    public ResponseResult<CustomModel> modifyCustom(
            @ApiParam(name = "接口模型入参", value = "传入json格式", required = true)
            @RequestBody CustomModel model) throws Exception {
        model.setCustomType(null);
        customService.modifyCustom(model);
        return ResponseResultFactory.ucc("修改成功！");
    }


    @ApiOperation("管理员修改商户信息")
    @PostMapping("/admin/modify")
    public ResponseResult<CustomModel> adminModifyCustom(
            @ApiParam(name = "接口模型入参", value = "传入json格式", required = true)
            @RequestBody CustomAdminReq model) throws Exception {
        customService.adminModifyCustom(model);
        return ResponseResultFactory.ucc("修改成功！");
    }

    @ApiOperation("修改凭证信息")
    @PostMapping("/modifyCert")
    public ResponseResult<CustomModel> modifyCert(
            @ApiParam(name = "接口模型入参", value = "传入json格式", required = true)
            @RequestBody CustCertModel model) {
        customService.modifyCert(model);
        return ResponseResultFactory.ucc("修改成功！");
    }

    @ApiOperation("删除商户信息")
    @GetMapping("/remove/{id}")
    public ResponseResult<Object> remove(
            @ApiParam(name = "记录ID", value = "传入String类型", required = true)
            @PathVariable("id") String id) {
        customService.removeCustom(id);
        return ResponseResultFactory.ucc("移除成功！");
    }

    @ApiOperation("查询商户信息列表")
    @PostMapping("/list")
    public ResponseResult<ReturnList<CustomRsp>> queryCustomList(
            @ApiParam(name = "查询条件", value = "传入json类型", required = true)
            @RequestBody PageQuery<CustomQuery> params){
        ReturnList<CustomRsp> result = customService.queryCustomList(params);
        return ResponseResultFactory.ucc("查询成功！", result);
    }


    @ApiOperation("查询所有商户不分页")
    @PostMapping("/list-all")
    public ResponseResult<List<CustomRsp>> queryCustomList(@RequestBody CustomQuery query){
        List<CustomRsp> result = customService.queryCustomAllList(query);
        return ResponseResultFactory.ucc("查询成功！", result);
    }


    @ApiOperation("查询商户信息详情")
    @GetMapping("/queryById/{id}")
    public ResponseResult<CustomRsp> queryCustomList(
            @ApiParam(name = "id", value = "传入json类型", required = true)
            @PathVariable String id){
        CustomRsp result = customService.queryByCustomId(id);
        return ResponseResultFactory.ucc("查询成功！", result);
    }


}
